package com.fh.shop.api.util;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class RedisUtil {

    public static void set(String key, String value) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void hset(String key, String field, String value) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.hset(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void hmset(String key, Map<String, String> map) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.hmset(key, map);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static String hget(String key, String field) {
        Jedis connection = null;
        String value = "";
        try {
            connection = RedisPool.getConnection();
            value = connection.hget(key, field);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
        return value;
    }

    public static boolean exist(String key) {
        Jedis connection = null;
        boolean exist = false;
        try {
            connection = RedisPool.getConnection();
            exist = connection.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
        return exist;
    }

    public static void expire(String key, int seconds) {
        Jedis connection = null;
        boolean exist = false;
        try {
            connection = RedisPool.getConnection();
            connection.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void setEx(String key, String value, int seconds) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.setex(key, seconds, value);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void delete(String key) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.del(key);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static String get(String key) {
        Jedis connection = null;
        String result = "";
        try {
            connection = RedisPool.getConnection();
            result = connection.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            // 不合适了，因为，缓存服务器宕机了
            // 我们还可以从数据库获取数据。
            // 如果在此处抛了异常，那么明明数据库是正常的，而且里面也有数据
            // 但就不会去查询数据库了。所以要把此处的抛异常给干掉。
//            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
        // 有throw了，就可以不要return
        // 否则，你就得加上return
        return result;
    }
}
